import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../pages/Login.vue'),
    meta: {
      title: '登录'
    }
  },
  {
    path: '/',
    name: 'Home',
    component: () => import('../pages/Home.vue'),
    meta: {
      title: '首页'
    }
  },
  {
    path: '/PostDetail',
    name: 'PostDetail',
    component: () => import('../pages/PostDetail.vue'),
    meta: {
      title: '文章详细'
    }
  },
  {
    path: '/ClassTest',
    name: 'ClassTest',
    component: () => import('../pages/ClassTest.vue'),
    meta: {
      title: '随堂测验'
    }
  },
  {
    path: '/myMistakes',
    name: 'myMistakes',
    component: () => import('../pages/myMistakes.vue'),
    meta: {
      title: '我的错题本'
    }
  },
  {
    path: '/exerciseMistakes',
    name: 'exerciseMistakes',
    component: () => import('../pages/exerciseMistakes.vue'),
    meta: {
      title: '复习错题'
    }
  },
  {
    path: '/note/home',
    name: '我的笔记',
    component: () => import('../pages/notePage/home.vue'),
    meta: {
      title: '我的笔记'
    }
  },
  {
    path: '/note/write',
    name: '记笔记',
    component: () => import('../pages/notePage/noteEditor.vue'),
    meta: {
      title: '记笔记'
    }
  },
  {
    path: '/getLicense/home',
    name: '考证专区',
    component: () => import('../pages/getLicense/home.vue'),
    meta: {
      title: '考证专区'
    }
  },
  {
    path: '/getLicense/detailPage',
    name: '考证专区',
    component: () => import('../pages/getLicense/detailPage.vue'),
    meta: {
      title: '考证专区'
    }
  },
  {
    path: '/wirtePost',
    name: 'wirtePost',
    component: () => import('../pages/wirtePost.vue'),
    meta: {
      title: '写文章'
    }
  },
  {
    path: '/category',
    name: 'category',
    component: () => import('../pages/category.vue'),
  },
  {
    path: '/searchPost',
    name: 'searchPost',
    component: () => import('../pages/SearchPost.vue'),
  },
  {
    path: '/personCenter/:userId',
    name: 'personCenter',
    component: () => import('../pages/user/person-center.vue'),
    meta: {
      title: '个人中心'
    },
    children: [
      {
        path: '',
        name: 'myPost',
        component: () => import('../pages/user/components/page/my-post.vue'),
      },
      {
        path: 'MyCollect',
        name: 'myCollect',
        component: () => import('../pages/user/components/page/my-collect.vue'),
      }
    ]
  },
  {
    path: '/personCenter/:userId/account',
    name: 'userManage',
    component: () => import('../pages/user/extends/user-manage.vue'),
    meta: {
      title: '用户管理'
    },
    children: [
      {
        path: '',
        name: 'profile',
        component: () => import('../pages/user/extends/children/profile.vue'),
      },
      {
        path: 'face',
        name: 'face',
        component: () => import('../pages/user/extends/children/face.vue'),
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  if (typeof to.meta.title === 'function') {
    document.title = to.meta.title(to);
  } else {
    document.title = to.meta.title || '默认标题';
  }
  next();
});

export default router
